const CallFunction = wx.cloud.callFunction
class Interface {
	constructor() {}

	/**
    @description 访问云函数 <get_meta>
    @return { String } openid
   */
	async GetMeta() {
		const result = await CallFunction({
			name: 'get_meta',
		})

		return result.result.data
	}

	/**
    @description 访问云函数 <login>
   */
	async Login({ username, password }) {
		const result = await wx.cloud.callFunction({
			name: 'login',
			data: { username, password },
		})

		return result.result
	}

	/**
    @description 访问云函数 <add_user_request>
   */
	async AddUserRequest({ name, role, username, password, identification }) {
		const result = await CallFunction({
			name: 'add_user_request',
			data: { name, role, username, password, identification },
		})
	}

	/**
      @description 访问云函数<get_requests> - 获取所有注册请求
     */
	async GetRequests() {
		const result = await CallFunction({
			name: 'get_requests',
		})

		return result.result.data
	}

	/**
      @description 访问云函数<add_class> - 创建班级
     */
	async AddClass({ name, course, host, limit, code }) {
		const result = await CallFunction({
			name: 'add_class',
			data: { name, course, host, limit, code },
		})
		return result
	}

	/**
      @description 访问云函数<get_class> - 获取单个班级信息
      */
	async GetClass({ id, name, course, host }) {
		const result = await CallFunction({
			name: 'get_class',
			data: { id, name, course, host },
		})
		return result.result.data
	}

	/**
      @description 访问云函数<get_classes>
        学生 - 获取全部已经加入的班级
        教师 - 获取全部创建的班级
     */
	async GetClasses(role) {
		const result = await CallFunction({
			name: 'get_classes',
			data: { role },
		})

		return result.result
	}

	/**
      @description 访问云函数 <add_student> 学生用户加入班级
     */
	async AddStudent(id) {
		const name = wx.getStorageSync('name')
		const avatar = wx.getStorageSync('avatar')
		const identification = wx.getStorageSync('identification')
		const openid = wx.getStorageSync('openid')
		const result = await CallFunction({
			name: 'add_student',
			data: { id, name, avatar, identification },
		})

		return result
	}

	/**
      @description 访问云函数 <get_user> 获取用户
     */
	async GetUser({ role, openid }) {
		const result = await CallFunction({
			name: 'get_user',
			data: { role, openid },
		})

		return result.result.data[0]
	}

	/**
      @description 访问云函数 <update_user> 修改用户信息
     */
	async UpdateUser({ id, name, identification, username, password, avatar }) {
		const result = await CallFunction({
			name: 'update_user',
			data: { id, name, identification, username, password, avatar },
		})
	}

	/**
      @description 访问云函数 <add_sign> 创建签到
     */
	async AddSign({ class_id, code, name }) {
		const result = await CallFunction({
			name: 'add_sign',
			data: { class_id, code, name },
		})
	}

	/**
    @description 访问云函数 <get_signs> 获取对应班级的全部签到任务
   */
	async GetSigns(id) {
		const result = await CallFunction({
			name: 'get_signs',
			data: { id },
		})

		return result.result.data
	}

	/**
      @description 访问云函数 <add_sign_record> 添加签到记录
     */
	async AddSignRecord({ class_id, sign_id }) {
		const result = await CallFunction({
			name: 'add_sign_record',
			data: { class_id, sign_id },
		})
		console.log('%c签到结果', 'color: yellow', result)
	}

	/**
    @description 访问云函数 <get_my_sign_record> 获取个人签到记录
   */
	async GetSignRecords() {
		const result = await CallFunction({
			name: 'get_sign_records',
		})
		return result.result.data
	}

	/**
    @description 访问云函数 <add_notice> 添加公告
   */
	async AddNotice({ class_id, content, attach }) {
		const result = await CallFunction({
			name: 'add_notice',
			data: { class_id, content, attach },
		})
		return result
	}

	/**
    @description 访问云函数 <get_notices> 通过 class_id 获取公告
   */
	async GetNotices(class_id) {
		const result = await CallFunction({
			name: 'get_notices',
			data: { class_id },
		})
		return result.result.data
	}

	/**
    @description 访问云函数 <add_test> 创建测试
   */
	async AddTest({ _id, name, class_id, class_name, has_file }) {
		const result = await CallFunction({
			name: 'add_test',
			data: { _id, name, class_id, class_name, has_file },
		})

		return result
	}

	/**
    @description 访问云函数 <add_titles> 批量添加题目
   */
	async AddTitles({ titles, test_id }) {
		const result = await CallFunction({
			name: 'add_titles',
			data: { titles, test_id },
		})

		return result
	}

	/**
    @description 访问云函数 <get_tests_student> 获取学生用户可以做的测试
   */
	async GetTestsStudent() {
		const result = await CallFunction({
			name: 'get_tests_student',
		})
		return result.result
	}

  	/**
    @description 访问云函数 <get_tests_teacher> 获取学生用户可以做的测试
   */
	async GetTestsTeacher() {
		const result = await CallFunction({
			name: 'get_tests_teacher',
		})
		return result.result
	}


	/**
    @description 访问云函数 <get_test> 根据测试"test_id"获取对应测试及题目
   */
	async GetTest(test_id) {
		const result = await CallFunction({
			name: 'get_test',
			data: { test_id },
		})
		return result.result
	}

	/**
    @description 访问云函数 <add_test_record> 添加答题记录 
   */
	async AddTestRecord({ answer, test_id }) {
		const identification = wx.getStorageSync('identification'),
			name = wx.getStorageSync('name'),
			avatar = wx.getStorageSync('avatar')
		const result = await CallFunction({
			name: 'add_test_record',
			data: {
				name,
				avatar,
				answer,
				test_id,
				identification,
			},
		})
		return result
	}

	/**
    @description 访问云函数 <add_file> 添加上传文件的记录
   */
	async AddFile({ test_id, path, alias, class_id, suffix, filename }) {
		const identification = wx.getStorageSync('identification'),
			name = wx.getStorageSync('name'),
			avatar = wx.getStorageSync('avatar'),
			role = wx.getStorageSync('role'),
      openid = wx.getStorageSync('openid')
		const result = await CallFunction({
			name: 'add_file',
			data: {
				test_id,
				class_id,
				alias,
				name,
        identification,
        role,
        avatar,
				path,
        suffix,
        filename
			},
		})
	}

  /**
    @description 访问云函数 <get_test_records> 根据测试id获取所有答题记录
   */
  async GetTestRecords(test_id) {
    const result = await CallFunction({
      name: 'get_test_records',
      data: { test_id }
    })

    return result.result
  }

  /**
    @description 访问云函数 <get_test_record> 根据 test_id 和 openid 获取对应答题详情
   */
  async GetTestRecord({ test_id, openid }) {
    const result = await CallFunction({
      name: 'get_test_record',
      data: { test_id, openid }
    })

    return result.result
  }

  /**
    @description 访问云函数 <update_score> 根据 class_id 和 openid 更新对应班级学生的平时分数
   */
  async UpdateScore({ class_id, openid, score }) {
    const result = await CallFunction({
      name: 'update_score',
      data: { class_id, openid, score }
    })
    
    return result
  }

  /**
    @description 访问云函数 <get_class_detail> 获取班级详情
   */
   async GetClassDetail({ class_id, role }) {
     const result = await CallFunction({
       name: 'get_class_detail',
       data: { class_id, role }
     })

     return result.result
   }

  /**
    @description 访问云函数 <add_question> 添加论坛问题 
   */
   async AddQuestion({ images, content }) {
     const name = wx.getStorageSync('name')
     const avatar = wx.getStorageSync('avatar')
     const result = await CallFunction({
       name: 'add_question',
       data: { content, images, name, avatar }
     })

     return result
   }

  /**
    @description 访问云函数 <get_questions> 获取所有论坛问题 
   */
   async GetQuestions() {
     const result = await CallFunction({ name: 'get_questions' })
     return result.result.data
   }

   /**
    @description 访问云函数 <get_comments> 获取帖子所有评论 
   */
   async GetComments(question_id) {
     const result = await CallFunction({ name: 'get_comments', data: { question_id } })

     return result.result.data
   }

   /**
    @description 访问云函数 <add_comment> 添加评论 
   */
   async AddComment({ question_id, content}) {
     const name = wx.getStorageSync('name')
     const avatar = wx.getStorageSync('avatar')
     const role = wx.getStorageSync('role')

     const result = await CallFunction({ name: 'add_comment', data: { name, avatar, question_id, content, role: role * 1 } })

     return result
   }
  
}

export const Serve = new Interface()
